package com.party.common;

public class PageTool2 {
    private String url;
    private Page page;
    private int currentPage = 1;
    private int pageSize = 10;
    private int totalPageCount;
    private int cursor = 5;
    private int lineCursor = 10;

    private String urlString;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public PageTool2() {
    }

    public String getPageString(String url, Page page) {
        if (url.indexOf("?") != -1) {
            this.url = "<a class='not_current_page'href='" + url + "&page=";
            this.urlString = url + "&page=";
        } else {
            this.url = "<a class='not_current_page'href='" + url + "?page=";
            this.urlString = url + "?page=";
        }
//        this.url = "<a class='not_current_page'href='" + url + "?page=";
        this.page = page;
        this.currentPage = page.getCurrentPage();
        this.pageSize = page.getPageSize();
        this.totalPageCount = page.getTotalPageCount();
        return "共 " + page.getTotalSize() + " 条记录  &nbsp" + getLeftTools() + getCenter() + getRightTools() + "";
    }

    private String getLeftTools() {
        StringBuffer leftString;
        if (currentPage == 1) {
            leftString = new StringBuffer("");
        } else {
            leftString = new StringBuffer(getUrl() + "1'><span class=''>« 首页</span></a>" + getUrl() + (getCurrentPage() - 1) + "'<span class=''> &nbsp;&nbsp;上一页&nbsp;&nbsp;</span></a>");
        }
        return leftString.toString();
    }

    private String getRightTools() {
        StringBuffer rightString;
        String select = " &nbsp;  第<select id=\"PB_Page_Select\" onchange=\"window.location.href='" + this.urlString + "'+this.value; \">";
        for(int i=1;i<=this.getTotalPageCount();i++){
            if(i==getCurrentPage()){
                select += "<option value=\"" + i + "\" selected=\"true\">" + i + "</option>";
            }else {
                select += "<option value=\"" + i + "\" >" + i + "</option>";
            }
        }
        select += "</select>页" ;
        if (currentPage == totalPageCount || totalPageCount == 0) {
            rightString = new StringBuffer(select);
        } else {
            rightString = new StringBuffer(getUrl() + (getCurrentPage() + 1) + "'><span class=''> &nbsp;&nbsp;下一页&nbsp;&nbsp;</span></a>" + getUrl() + (getTotalPageCount()) + "'<span class=''>&nbsp;&nbsp;尾页 »</span></a>&nbsp&nbsp" + select);
        }
        return rightString.toString();
    }

    private String getCenter() {
        StringBuffer centerString = new StringBuffer("");
        if (totalPageCount <= lineCursor) {
            for (int i = 1; i < this.totalPageCount + 1; i++) {
                if (i == this.currentPage) {
                    centerString.append("<a href='javascript:;' class='on'>[" + i + "]</a>");
                } else {
                    centerString.append(getUrl() + i + "'>" + i + "</a>  ");
                }
            }
        } else {
            int s = currentPage - cursor;
            if (s < 0) {
                for (int i = 1; i < lineCursor + 1; i++) {
                    if (i == this.currentPage) {
                        centerString.append("<a href='javascript:;' class='on'>[" + i + "]</a>");
                    } else {
                        centerString.append(getUrl() + i + "'>[" + i + "]</a>  ");
                    }
                }
            } else {
                if (s + lineCursor > totalPageCount) {
                    for (int i = totalPageCount - lineCursor + 1; i <= totalPageCount; i++) {
                        if (i == this.currentPage) {
                            centerString.append("<a href='javascript:;' class='on'>[" + i + "]</a>");
                        } else {
                            centerString.append(getUrl() + i + "'>[" + i + "]</a> ");
                        }
                    }
                } else {
                    for (int i = s + 1; i < lineCursor + s + 1; i++) {
                        if (i == this.currentPage) {
                            centerString.append("<a href='javascript:;' class='on'>[" + i + "]</a>");
                        } else {
                            centerString.append(getUrl() + i + "'>[" + i + "]</a> ");
                        }
                    }
                }
            }
        }
        return centerString.toString();
    }
}
